package com.fjb.controller.pay;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjb.common.entity.JsonResult;
import com.fjb.controller.base.BaseController;
import com.fjb.dto.LoginUserInfo;
import com.fjb.service.pay.WxPayMyService;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;

/**
 * @Description:微信回调
 * @author hemiao
 * @time:2020年2月9日 下午10:53:21
 */
@Controller
@RequestMapping("/wxPay")
public class WxPayController extends BaseController{
	
	@Autowired
    private WxPayMyService wxPayMyService;
	
	/**
	 * @Description:订单支付
	 * @param request
	 * @return
	 * JsonResult<List<ShoppingCart>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月9日 下午10:54:09
	 */
	@RequestMapping(value="/addOrderPay")
	@ResponseBody
	public JsonResult<WxPayMpOrderResult> addOrderPay(HttpServletRequest request,Integer orderId){
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		return wxPayMyService.addOrderPay(request, orderId, userInfo);
	}
	
	/**
	 * @Description:更新 微信支付回调
	 * @param request
	 * @param orderId
	 * @return
	 * JsonResult<Object>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月9日 下午11:11:15
	 */
	@RequestMapping(value="/updateNotify")
	@ResponseBody
	public String updateNotify(HttpServletRequest request){
		String xmlResult = null;
        try {
            xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        } catch (IOException e) {
            e.printStackTrace();
            return WxPayNotifyResponse.fail(e.getMessage());
        }	
        return wxPayMyService.updateOrderNotify(xmlResult);
	}
}
